//
//  HYUserAccount.swift
//  WeiBo
//
//  Created by crazyfish on 16/3/12.
//  Copyright © 2016年 crazyfish. All rights reserved.
//

import UIKit

class HYUserAccount: NSObject,NSCoding {

   /**
    "access_token": "ACCESS_TOKEN",
    "expires_in": 1234,
    "remind_in":"798114",
    "uid":"12341234"
    */
    
    //访问令牌
    var access_token: String?
    //生命周期(秒数)从->accesstoken获取回来的时候开始计算
    var expires_in: Double = 0 {
        
        didSet{
            
            expiresDate = NSDate().dateByAddingTimeInterval(expires_in)
        }
        
    }
    //过期的时间
    var expiresDate: NSDate?
    //过期时间
    var remind_in: String?
    //用户id
    var uid: String?
    // 	用户昵称
    var screen_name: String?
    //用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    //字典转模型的方法
    init(dict:[String: AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
        
    }
    
    //防止KVC崩溃
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    //重写description方法(这个方法没有智能提示)
    override var description: String {
        
        //keys是一个数组,里面存放的是模型的属性
        let keys = ["access_token","expires_in","remind_in","uid","avatar_large"]
        
        //模型转字典方法(用来打印字典转模型后的模型数据)
        return dictionaryWithValuesForKeys(keys).description
    }

    //0.把模型数据归档和解档
    //1.归档
    /**
    //访问令牌
    var access_token: String?
    //生命周期(秒数)
    var expires_in: Int = 0
    //过期时间
    var remind_in: String?
    //用户id
    var uid: String?
    // 	用户昵称
    var screen_name: String?
    //用户头像地址（大图），180×180像素
    var avatar_large: String?
    */

    //1.归档
    func encodeWithCoder(enCoder: NSCoder) {
        
        enCoder.encodeObject(access_token, forKey: "access_token")
        enCoder.encodeObject(remind_in, forKey: "remind_in")
        enCoder.encodeObject(uid, forKey: "uid")
        enCoder.encodeObject(avatar_large, forKey: "avatar_large")
        enCoder.encodeDouble(expires_in, forKey: "expires_in")
        enCoder.encodeObject(expiresDate, forKey: "expiresDate")
        
    }
    //2.解档
    required init?(coder aDecoder: NSCoder) {
        
        uid = aDecoder.decodeObjectForKey("uid") as? String
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        remind_in = aDecoder.decodeObjectForKey("remind_in") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        
    }
    
    
    
    
}
